import math


def sign(a):
    return (a > 0) - (a < 0)


# 万有引力函数
def gravity(p1, p2):
    m1, x1, y1, v_x1, v_y1 = p1
    m2, x2, y2, v_x2, v_y2 = p2
    if (x1, y1) == (x2, y2):    # 发生碰撞，其中一颗星球被吞噬了
        return p1, p2
    delta_x = x1 - x2
    delta_y = y1 - y2
    # 距离平方
    r2 = delta_x ** 2 + delta_y ** 2
    # 万有引力定律
    F = 6.67259e-17 * m1 * m2 / r2
    # 星球夹角
    theta = math.acos(delta_x / r2 ** 0.5)
    # x、y 轴引力分量
    fx = abs(F * math.cos(theta)) * sign(delta_x)
    fy = abs(F * math.sin(theta)) * sign(delta_y)
    # x、y 轴加速度，牛顿第二定律
    ax2 = fx / m2
    ay2 = fy / m2
    ax1 = fx / m1
    ay1 = fy / m1
    # 速度变化，vt = v0 + at
    v_x2t = v_x2 + ax2 * 1
    v_y2t = v_y2 + ay2 * 1
    v_x1t = v_x1 - ax1 * 1
    v_y1t = v_y1 - ay1 * 1
    # 位置变化，st = (v0 + vt)/2*t
    x2 += (v_x2+v_x2t) / 2 * 1
    y2 += (v_y2+v_y2t) / 2 * 1
    x1 -= (v_x1+v_x1t) / 2 * 1
    y1 -= (v_y1+v_y1t) / 2 * 1
    p3 = [m1, x1, y1, v_x1, v_y1]
    p4 = [m2, x2, y2, v_x2, v_y2]
    return p3, p4


def task1(planets_num, check_time, planet_list):
    t = 1
    while 1:
        for i in range(planets_num):
            for j in range(i+1, planets_num):
                planet_list[i], planet_list[j] = gravity(planet_list[i], planet_list[j])
        if t == check_time:
            return list(map(lambda x: x[1:3], planet_list))
        t += 1


def func1():
    planets = [
        [10000, 0, 0, 0, 0],
        [0.1, 1000, 0, 0, math.sqrt(10)],
    ]
    return task1(2, 1986, planets)


def task2(check_time, planet_list):
    dic = {
        0: '永夜',
        1: '稳定纪元',
        2: '双日时期',
        3: '三日时期',
    }
    t = 1
    while 1:
        count_sun = 0       # 影响行星的太阳数量计数
        for i in range(4):
            for j in range(i+1, 4):
                if j == 3:     # 涉及行星的情况
                    distance = (planet_list[i][1] - planet_list[j][1])**2 + (planet_list[i][2] - planet_list[j][2])**2
                    if distance > 40000:    # 太远
                        continue
                    else:
                        count_sun += 1
                planet_list[i], planet_list[j] = gravity(planet_list[i], planet_list[j])
        if t == check_time:
            return dic[count_sun]
        t += 1


def func2():
    planets = [
            [1000, 0, 0, 0, 0],
            [1, 1000000, 0, 0, 0],
            [1, -1000000, 0, 0, 0],
            [0.1, 100, 0, 0, math.sqrt(10)]
        ]
    return task2(1986, planets)


def task3(check_time, planet_list):
    score = 0       # 文明进程得分
    t = 1
    while 1:
        count_sun = 0       # 影响行星的太阳数量计数
        for i in range(4):
            for j in range(i+1, 4):
                if j == 3:     # 涉及行星的情况
                    distance = (planet_list[i][1] - planet_list[j][1])**2 + (planet_list[i][2] - planet_list[j][2])**2
                    if distance > 40000:    # 太远
                        continue
                    else:
                        count_sun += 1
                planet_list[i], planet_list[j] = gravity(planet_list[i], planet_list[j])
        # 计算得分
        if count_sun == 1:
            score += 2
        else:
            score -= 1
        if score < 0:       # 文明消失不可逆，直接返回了
            return '文明消失'
        if t == check_time:
            # 判断文明进程
            if score < 400:
                return '1级文明'
            elif score < 1200:
                return '2级文明'
            else:
                return '3级文明'
        t += 1


def func3():
    planets = [
        [1000, 0, 0, 0, 0],
        [1, 1000000, 0, 0, 0],
        [1, -1000000, 0, 0, 0],
        [0.1, 100, 0, 0, math.sqrt(10)]
    ]
    return task3(198, planets)


def bonus():
    pass


if __name__ == '__main__':
    print(func1())
    print(func2())
    print(func3())